//
//  CWQ_tableView.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/10/13.
//  Copyright © 2016年 陈武琦. All rights reserved.
//




#import "CWQ_tableView.h"
#import "UITableView+FDTemplateLayoutCell.h"


@implementation CWQ_tableViewInterceptor

//
#pragma mark - forward & response override
//将无法处理的selector转发给其他对象
- (id)forwardingTargetForSelector:(SEL)aSelector {
    if ([self.middleMan respondsToSelector:aSelector]) return self.middleMan;
    if ([self.dataSourceReceiver respondsToSelector:aSelector]) return self.dataSourceReceiver;
    return [super forwardingTargetForSelector:aSelector];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    if ([self.middleMan respondsToSelector:aSelector]) return YES;
    if ([self.dataSourceReceiver respondsToSelector:aSelector]) return YES;
    return [super respondsToSelector:aSelector];
}

@end



@interface CWQ_tableView ()

@property (nonatomic, strong) CWQ_tableViewInterceptor *tableViewInterceptor;
@property (nonatomic, copy) NSString *cellID;
@property (nonatomic, copy) NSString *cellClass;
@property (nonatomic, copy) NSArray *sourceArray;
@end

@implementation CWQ_tableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style cellClass:(NSString *)cellClass vc:(id)vc upload:(BOOL)upload{

    if (self = [super initWithFrame:frame style:style]) {
        
//        self.buttonText = @"";
//        self.buttonNormalColor = [UIColor redColor];
//        //        self.buttonHighlightColor = [UIColor yellowColor];
//        self.loadedImageName = @"nocontent";
//        self.descriptionText = @"暂无数据 - -!";
//        self.dataVerticalOffset = 0;
    
    
        _cellClass = cellClass;
        _cellID = [cellClass stringByAppendingString:@"ID"];
        self.dataSource = vc;
        self.delegate = vc;
        self.requestDelegate = vc;

        [self setupCanUpLoad:upload];
    }
    return self;

}

- (void)setupCanUpLoad:(BOOL)upLoad {
    
    self.backgroundColor = kAppBg;
    self.tableFooterView = [UIView new];
    self.showsVerticalScrollIndicator = NO;
//    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self registerNib:[UINib nibWithNibName:_cellClass bundle:[NSBundle mainBundle]] forCellReuseIdentifier:_cellID];
    
    weakSelf_define;
    // 添加下拉刷新头部控件
    [self addLegendHeaderWithRefreshingBlock:^{
        //请求加载数据
        [weakSelf cwq_requestDataWithDown:YES];
    }];
    if (upLoad) {
        [self addLegendFooterWithRefreshingBlock:^{
            //请求加载数据
            [weakSelf cwq_requestDataWithDown:NO];
        }];
        
    }

}

- (void)cwq_requestDataWithDown:(BOOL)down {
    
    if ([self.requestDelegate respondsToSelector:@selector(cwq_tableView:requestDataWithDown:)]) {
        [self.requestDelegate cwq_tableView:self requestDataWithDown:down];
    }
}

- (void)setupSourceArray:(NSArray *)array {
    _sourceArray = array;
    [self reloadData];
}

#pragma mark - DataSource Delegate Setter/Getter Override
- (void)setDataSource:(id<UITableViewDataSource>)dataSource {
    self.tableViewInterceptor.dataSourceReceiver = dataSource;
    [super setDataSource:(id<UITableViewDataSource>)self.tableViewInterceptor];
}

- (void)setDelegate:(id<UITableViewDelegate>)delegate {
    self.tableViewInterceptor.delegateReceiver = delegate;
    [super setDelegate:(id<UITableViewDelegate>)self.tableViewInterceptor];

}






- (CWQ_tableViewInterceptor *)tableViewInterceptor {
    if (!_tableViewInterceptor) {
        _tableViewInterceptor = [[CWQ_tableViewInterceptor alloc]init];
        _tableViewInterceptor.middleMan = self;
    }
    return _tableViewInterceptor;
}


#pragma mark - Delegate Method Override
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.tableViewInterceptor.dataSourceReceiver respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
       return [self.tableViewInterceptor.dataSourceReceiver tableView:tableView numberOfRowsInSection:section];
    }
    
    return self.sourceArray.count;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.tableViewInterceptor.dataSourceReceiver respondsToSelector:@selector(tableView:editingStyleForRowAtIndexPath:)]) {
        return [self.tableViewInterceptor.dataSourceReceiver tableView:tableView editingStyleForRowAtIndexPath:indexPath];
    }

    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    id model = self.sourceArray[indexPath.row];
    if ([self.tableViewInterceptor.dataSourceReceiver respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) {
        return [self.tableViewInterceptor.dataSourceReceiver tableView:tableView heightForRowAtIndexPath:indexPath];
    }
    
    CGFloat height = [self fd_heightForCellWithIdentifier:_cellID configuration:^(UITableViewCell *cell) {
        [cell setValue:model forKey:@"model"];
    }];
    return height;
}


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.tableViewInterceptor.dataSourceReceiver respondsToSelector:@selector(tableView:cellForRowAtIndexPath:)]) {
        return [self.tableViewInterceptor.dataSourceReceiver tableView:tableView cellForRowAtIndexPath:indexPath];
    }
    
    id model = self.sourceArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setValue:model forKey:@"model"];
    return cell;
}





@end
